package org.jradweb.tools.generator.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
/**
 * 
 *
 * Created On 2008-2-29
 * @author <a href="mailto:wagzhi@gmail.com">Paul Wang</a>
 * @version $Id: PropertiesLoader.java 148 2008-10-10 03:07:23Z wagzhi $
 */
public class PropertiesLoader {
	public static final String  CONFIG_HOME_PROPERTY_NAME="unitils.configuration.configHomePropertyName";
	
	
	public static final String DATABASE_DRIVER_CLASS_NAME = "database.driverClassName";

	public static final String DATABASE_URL = "database.url";

	public static final String DATABASE_USER_NAME = "database.userName";

	public static final String DATABASE_PASSWORD = "database.password";

	public static final String DATABASE_SCHEMA_NAME = "database.schemaNames";

	public static final String DATABASE_DIALECT = "database.dialect";
	
	
	private static final Logger log=Logger.getLogger(PropertiesLoader.class);
	
	private File baseDir;
	private String artifactId;
	
	public PropertiesLoader(File baseDir,String artifactId){
		this.baseDir=baseDir;
		this.artifactId=artifactId;
	}
	
	public PropertiesLoader(File baseDir){
		this.baseDir=baseDir;
	}
	
	public Properties loadTestProperties() throws FileNotFoundException,
	IOException {
		String propFile = baseDir.getAbsolutePath() + "/src/test/resources/unitils.properties";
		java.util.Properties props = new Properties();
		props.load(new FileInputStream(new File(propFile)));
		
		File extFile=this.getExtendTestPropertiesFile(props);
		if(extFile!=null){				
			if(extFile.exists()){
				Properties extProps=new Properties();
				extProps.load(new FileInputStream(extFile));
				props.putAll(extProps);
				log.info("Loaded extended properties from file \""+extFile.getAbsolutePath()+"\"");
			}
			else{
				log.warn("Extended properties file \""+extFile.getAbsolutePath()+"\" not exists!");
			}
			
		}
		
		return props;
	}
	
	public File getExtendTestPropertiesFile(Properties testProperties){
		String configHomeName=testProperties.getProperty(CONFIG_HOME_PROPERTY_NAME);
		if(configHomeName!=null){
			String configHome=System.getProperty(configHomeName);
			log.info("System property: "+configHomeName+"="+configHome);
			
			File file=new File(configHome,"test-"+artifactId+".properties");			
			return file;
		}
		else return null;
	}
	
	
	/**
	 * load properties from ${artifactId}.properties, add add propertis for unitils
	 */
	public Properties loadProperties() throws FileNotFoundException,
			IOException {
		String propFile = baseDir.getAbsolutePath() + "/src/main/resources/"
				+ artifactId + ".properties";
		
		java.util.Properties props = new Properties();
		props.load(new FileInputStream(new File(propFile)));
		
		
		//set properties for unitils, convert from hibernate properties
		props.setProperty("database.driverClassName", props.getProperty("hibernate.connection.driver_class"));
		props.setProperty("database.url", props.getProperty("hibernate.connection.url"));
		props.setProperty("database.userName", props.getProperty("hibernate.connection.username"));
		props.setProperty("database.password", props.getProperty("hibernate.connection.password"));
		
		String hibernateDialect=props.getProperty("hibernate.dialect");
		if(hibernateDialect==null||
				hibernateDialect.equals("org.hibernate.dialect.MySQLInnoDBDialect")||
				hibernateDialect.equals("org.hibernate.dialect.MySQLDialect"))
		{
			props.setProperty("database.dialect","mysql");
		}
		else //TODO add other database dialect
		{
			props.setProperty("database.dialect", "mysql");
		}
		log.info(
				"database.driverClassName="
						+ props.getProperty("database.driverClassName"));
		log.info("database.url=" + props.getProperty("database.url"));
		log.info(
				"database.userName=" + props.getProperty("database.userName"));
		log.info(
				"database.password=" + props.getProperty("database.password"));
		log.info(
				"database.schemaNames="
						+ props.getProperty("database.schemaNames"));
		log.info(
				"database.dialect=" + props.getProperty("database.dialect"));
		return props;

	}
	/**
	 * load properties from project.properties from resources path, add add propertis for unitils
	 */
	public Properties loadProjectProperties() throws FileNotFoundException,
			IOException {
		String propFile = baseDir.getAbsolutePath() + "/src/main/resources/project.properties";
		
		java.util.Properties props = new Properties();
		props.load(new FileInputStream(new File(propFile)));
		
		log.info("load properties from file: "+propFile);
		
		//set properties for unitils, convert from hibernate properties
		props.setProperty("database.driverClassName", props.getProperty("hibernate.connection.driver_class"));
		props.setProperty("database.url", props.getProperty("hibernate.connection.url"));
		props.setProperty("database.userName", props.getProperty("hibernate.connection.username"));
		props.setProperty("database.password", props.getProperty("hibernate.connection.password"));
		
		String hibernateDialect=props.getProperty("hibernate.dialect");
		if(hibernateDialect==null||
				hibernateDialect.equals("org.hibernate.dialect.MySQLInnoDBDialect")||
				hibernateDialect.equals("org.hibernate.dialect.MySQLDialect"))
		{
			props.setProperty("database.dialect","mysql");
		}
		else //TODO add other database dialect
		{
			props.setProperty("database.dialect", "mysql");
		}
		log.info(
				"database.driverClassName="
						+ props.getProperty("database.driverClassName"));
		log.info("database.url=" + props.getProperty("database.url"));
		log.info(
				"database.userName=" + props.getProperty("database.userName"));
		log.info(
				"database.password=" + props.getProperty("database.password"));
		log.info(
				"database.schemaNames="
						+ props.getProperty("database.schemaNames"));
		log.info(
				"database.dialect=" + props.getProperty("database.dialect"));
		return props;

	}

}
